package cn.wolfcode.trip.admin.controller;


import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/strategy")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IRegionService regionService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") QueryObject qo, Model model) {
        model.addAttribute("result", strategyService.queryForList(qo));
        model.addAttribute("regions", regionService.listAll());
        return "strategy/list";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Strategy strategy, MultipartFile pic) {
        if (pic!=null && pic.getContentType().startsWith("image") && pic.getSize()>0){
            String url = UploadUtil.upload(pic);
            strategy.setCoverUrl(url);
        }

        strategyService.saveOrUpdate(strategy);
        return "redirect:/strategy/list.do";
    }


}
